<?php

class Admin_SiteController extends Fengqiaoju_Controller_Action {

	public function init() {

		parent::init ();
		
		if ($this->userObject->getUserId () > 4 || ! $this->userObject->getUserId ()) {
			$this->message ( '无权操作' );
		}
		
		$this->view->do = $this->_request->getParam ( 'do' );
		$this->view->step = $this->_request->getParam ( 'step' );
		$this->view->site_id = $this->_request->getParam ( 'site_id' );
		$this->view->model_id = $this->_request->getParam ( 'model_id' );
		$this->view->site_seo_name = $this->_request->getParam ( 'site_seo_name' );
		$this->view->article_id = $this->_request->getParam ( 'article_id' );
		$this->view->page = $this->_request->getParam ( 'page' );
		$this->view->siteModel = $this->config ['model'] ['site'];
		$this->view->title = "后台管理";
	
	}

	/**
	 * 首页
	 */
	public function indexAction() {

		$siteObject = new Admin_Models_Site ();
		$where = array ();
		$where ['1'] = '1';
		$this->view->siteList = $siteObject->getSiteList ( $where );
	
	}

	/**
	 * 小站
	 */
	public function siteAction() {

		$siteObject = new Admin_Models_Site ();
		$categoryObject = new Admin_Models_SiteCategory ();
		
		//创建小站
		if ($this->view->do == 'create') {
			if ($this->view->step == 'post') {
				if ($siteObject->cerateSite ( $_POST )) {
					$this->ok ( '创建成功' );
				}
				else {
					$this->message ( '创建失败' );
				}
			}
			else {
				$where = '';
				$where = "category_fid=0";
				$this->view->categoryFirst = $categoryObject->fetchAll ( $where );
			}
		
		}
		elseif ($this->view->do == 'edit') { //编辑小站	
			

			//提交请求
			if ($this->getRequest ()->isPost ()) {
				$site = $where = array ();
				$site ['site_name'] = $_POST ['site_name'];
				$site ['site_seo_name'] = $_POST ['site_seo_name'];
				$site ['site_intro'] = $_POST ['site_intro'];
				$site ['category_id'] = $_POST ['category_id'];
				$where ['site_id'] = $_POST ['site_id'];
				if ($siteObject->updateSite ( $site, $where )) {
					$this->ok ( '编辑成功' );
				}
				else {
					$this->message ( '编辑失败' );
				}
			}
			else {
				$where = array ();
				$where ['site_id'] = $this->view->site_id;
				$this->view->siteInfo = $siteObject->getSiteInfo ( $where );
				
				$where = "category_fid=0";
				$this->view->categoryFirst = $categoryObject->fetchAll ( $where );
				$this->view->fid = $categoryObject->fetchRow ( 'category_id=' . intval ( $this->view->siteInfo ['category_id'] ) )->toArray();
				
			}
		}
	}

	/**
	 * 小站分类
	 */
	public function categoryAction() {

		$categoryObject = new Admin_Models_SiteCategory ();
		
		//创建分类
		if ($this->view->do == 'create') {
			if ($this->getRequest ()->isPost ()) {
				$data = array ();
				$data ['category_fid'] = $_POST ['category_fid'];
				$data ['category_name'] = $_POST ['category_name'];
				$data ['category_intro'] = $_POST ['category_intro'];
				if ($categoryObject->insert ( $data )) {
					$this->ok ( '创建成功' );
				}
				else {
					$this->message ( '创建失败' );
				}
			}
			else {
				$where = 'category_fid = 0';
				$this->view->categoryFirst = $categoryObject->fetchAll ( $where );
			}
		}
		elseif ($this->view->do == 'edit') { //编辑	
			//提交请求
			if ($this->getRequest ()->isPost ()) {
				$data = array ();
				$data ['category_fid'] = $_POST ['category_fid'];
				$data ['category_name'] = $_POST ['category_name'];
				$data ['category_intro'] = $_POST ['category_intro'];
				$where = "category_id=" . intval ( $_POST ['category_id'] );
				if ($categoryObject->update ( $data, $where )) {
					$this->ok ( '编辑成功' );
				}
				else {
					$this->message ( '编辑失败' );
				}
			}
			else {
				$where = "category_id=" . intval ( $this->_request->getParam ( 'category_id' ) );
				$this->view->categoryInfo = $categoryObject->fetchRow ( $where );
			}
		}
		elseif ($this->view->do == 'json') { //获取二级分类
			$where = 'category_fid = ' . intval ( $this->_request->get ( 'fid' ) );
			$cate1 [] = array ( 
				'category_id' => intval ( $this->_request->get ( 'fid' ) ) , 'category_name' => '请选择' 
			);
			
			$cate2 = $categoryObject->fetchAll ( $where )->toArray ();
			$cate = array_merge ( $cate1, $cate2 );
			echo Zend_Json::encode ( $cate );
			exit ();
		}
		else {
			$where = 'category_fid = 0';
			$this->view->categoryList = $categoryObject->fetchAll ( $where );
		}
	}

	/**
	 * 小站模块
	 */
	public function modelAction() {

		$modelObject = new Admin_Models_Model ();
		$siteObject = new Admin_Models_Site ();
		
		if ($this->view->site_id) {
			$where = array ();
			$where ['site_id'] = $this->view->site_id;
			$this->view->siteInfo = $siteObject->getSiteInfo ( $where );
		}
		if ($this->view->model_id) {
			
			$where = array ();
			$where ['model_id'] = $this->view->model_id;
			$this->view->modelInfo = $modelObject->getModelInfo ( $where );
			
			$where = array ();
			$where ['site_id'] = $this->view->modelInfo ['site_id'];
			$this->view->siteInfo = $siteObject->getSiteInfo ( $where );
		
		}
		
		//添加模块
		if ($this->view->do == 'addModel') {
			
			//提交请求
			if ($this->getRequest ()->isPost ()) {
				$data ['site_id'] = $this->_request->getPost ( 'site_id' );
				$data ['show_index'] = $this->_request->getPost ( 'show_index' );
				$data ['model_name'] = $this->_request->getPost ( 'model_name' );
				$data ['model_type'] = $this->_request->getPost ( 'model_type' );
				$data ['model_intro'] = $this->_request->getPost ( 'model_intro' );
				if ($siteObject->createModel ( $data )) {
					$this->ok ( '添加成功' );
				}
				else {
					$this->message ( '模块添加失败' );
				}
			}
		}
		elseif ($this->view->do == 'editModel') { //编辑模块
			//提交请求
			if ($this->getRequest ()->isPost ()) {
				$data = $where = array ();
				$where = 'model_id = ' . $this->_request->getPost ( 'model_id' );
				$data ['model_name'] = $this->_request->getPost ( 'model_name' );
				$data ['model_type'] = $this->_request->getPost ( 'model_type' );
				$data ['show_index'] = $this->_request->getPost ( 'show_index' );
				$data ['model_intro'] = $this->_request->getPost ( 'model_intro' );
				if ($modelObject->update ( $data, $where )) {
					$this->ok ( '编辑成功' );
				}
				else {
					$this->message ( '编辑失败' );
				}
			}
			else {
				$where = array ();
				$where ['model_id'] = $this->view->model_id;
				$this->view->modelInfo = $modelObject->getModelInfo ( $where );
			}
		}
		else {
			$where = array ();
			$where ['site_id'] = $this->view->site_id;
			$this->view->modelList = $modelObject->getModelList ( $where );
		}
	}

	/**
	 * 文章模块
	 */
	public function articleAction() {

		$articleObject = new Admin_Models_Article ();
		$modelObject = new Admin_Models_Model ();
		$siteObject = new Admin_Models_Site ();
		
		if ($this->view->model_id) {
			
			$where = array ();
			$where ['model_id'] = $this->view->model_id;
			$this->view->modelInfo = $modelObject->getModelInfo ( $where );
			
			$where = array ();
			$where ['site_id'] = $this->view->modelInfo ['site_id'];
			$this->view->siteInfo = $siteObject->getSiteInfo ( $where );
		
		}
		elseif ($this->view->article_id) {
			
			$where = array ();
			$where ['article_id'] = $this->view->article_id;
			$this->view->articleInfo = $articleObject->getArticleInfo ( $where );
			
			$where = array ();
			$where ['model_id'] = $this->view->articleInfo ['model_id'];
			$this->view->modelInfo = $modelObject->getModelInfo ( $where );
			
			$where = array ();
			$where ['site_id'] = $this->view->modelInfo ['site_id'];
			$this->view->siteInfo = $siteObject->getSiteInfo ( $where );
		}
		
		//添加文章
		if ($this->view->do == 'addArticle') {
			
			//提交请求
			if ($this->getRequest ()->isPost ()) {
				$data = array ();
				$data ['site_id'] = $this->_request->getPost ( 'site_id' );
				$data ['model_id'] = $this->_request->getPost ( 'model_id' );
				$data ['article_title'] = $this->_request->getPost ( 'article_title' );
				$data ['site_seo_name'] = $this->_request->getPost ( 'site_seo_name' );
				$data ['article_content'] = $this->_request->getPost ( 'article_content' );
				if ($articleObject->addArticle ( $data )) {
					$this->ok ( '文章添加成功' );
				}
				else {
					$this->message ( '编辑失败' );
				}
			}
		}
		elseif ($this->view->do == 'editArticle') { //编辑文章
			

			//提交请求
			if ($this->getRequest ()->isPost ()) {
				$data = $where = array ();
				$where ['article_id'] = $this->_request->getPost ( 'article_id' );
				$data ['article_title'] = $this->_request->getPost ( 'article_title' );
				$data ['article_content'] = $this->_request->getPost ( 'article_content' );
				if ($articleObject->updateArticle ( $data, $where )) {
					$this->ok ( '编辑成功' );
				}
				else {
					$this->message ( '编辑失败' );
				}
			}
			else {
			
			}
		}
		else { //文章列表
			$where = array ();
			//$where['site_id'] = $this->view->site_id;
			$where ['model_id'] = $this->view->model_id;
			$this->view->articleList = $articleObject->getArticleList ( $where );
		
		}
	}

	/**
	 * 单页面模块
	 */
	public function pageAction() {

		$modelObject = new Admin_Models_Model ();
		$siteObject = new Admin_Models_Site ();
		$pageObject = new Admin_Models_Page ();
		
		//获取模块信息
		$where = '';
		$where = 'model_id =' . intval ( $this->view->model_id );
		$this->view->modelInfo = $modelObject->fetchRow ( $where );
		
		//获取小站信息
		$where = '';
		$where = 'site_id =' . intval ( $this->view->modelInfo ['site_id'] );
		$this->view->siteInfo = $siteObject->fetchRow ( $where );
		
		//获取页面信息
		$where = '';
		$where = 'model_id=' . intval ( $this->view->model_id );
		$this->view->pageInfo = $pageObject->fetchRow ( $where );
		
		//页面存在,编辑页面
		if ($this->view->pageInfo ['page_id']) {
			
			//提交请求
			if ($this->getRequest ()->isPost ()) {
				$data = $where = array ();
				$where ['model_id'] = $this->_request->getPost ( 'model_id' );
				$data ['page_name'] = $this->_request->getPost ( 'page_name' );
				$data ['page_content'] = $this->_request->getPost ( 'page_content' );
				$where = $pageObject->arrayToString ( $where );
				if ($pageObject->update ( $data, $where )) {
					$this->ok ( '编辑成功' );
				}
				else {
					$this->message ( '页面编辑失败' );
				}
			}
		}
		else {
			
			//不存在,添加一个页面
			$data = array ();
			$data ['site_id'] = $this->view->siteInfo ['site_id'];
			$data ['model_id'] = $this->view->modelInfo ['model_id'];
			$data ['page_name'] = $this->view->modelInfo ['model_name'] . "内容";
			$data ['page_content'] = '';
			$data ['page_inputtime'] = date ( 'Y-m-d H::s' );
			$pageObject->insert ( $data );
		
		}
	}

	/**
	 * 照片模块
	 */
	public function photoAction() {

		$this->view->title = "照片管理";
		
		$modelObject = new Admin_Models_Model ();
		$siteObject = new Admin_Models_Site ();
		$photoObject = new Admin_Models_Photo ();
		
		//获取模块信息
		$where = array ();
		$where ['model_id'] = $this->view->model_id;
		$this->view->modelInfo = $modelObject->getModelInfo ( $where );
		
		//获取小站信息
		$where = array ();
		$where ['site_id'] = $this->view->modelInfo ['site_id'];
		$this->view->siteInfo = $siteObject->getSiteInfo ( $where );
		
		//上传照片
		if ($this->view->do == 'ajaxUpload') {
			$info = array ();
			$status = 0;
			$info = $this->upload ();
			
			if (is_array ( $info )) {
				$data = array ();
				$data ['photo_thumb'] = Fengqiaoju_Tool_Image::thumb ( $this->config ['url'] ['upload'] . $info ['filepath'], 120, 120 );
				$data ['photo_name'] = $info ['filename'];
				$data ['photo_path'] = $info ['filepath'];
				$data ['photo_inputtime'] = date ( 'Y-m-d H:i:s' );
				$data ['site_id'] = $this->view->modelInfo ['site_id'];
				$data ['model_id'] = $this->view->model_id;
				if ($photoObject->insert ( $data )) {
					$status = 1;
				}
			}
			
			//写日志
			$log .= "\r\n status:" . $status;
			$log .= "\r\n file:" . Zend_Json::encode ( $info );
			$log .= "\r\n data:" . Zend_Json::encode ( $data );
			$path = APPLICATION_PATH . "/../Log/site/upload/" . date ( 'Y-m' ) . '/';
			$this->writeLog ( $log, date ( 'd' ), 'site/upload' );
			
			echo $status;
			exit ();
		}
		
		//编辑照片
		if ($this->view->do == 'updatePhoto') {
			$status = 0;
			$name = $this->_request->getPost ( 'photo_name' );
			$intro = $this->_request->getPost ( 'photo_intro' );
			
			foreach ( $name as $id => $pname ) {
				$data = array ();
				$where = array ();
				$data ['photo_name'] = $pname;
				$data ['photo_intro'] = $intro [$id];
				$where = 'photo_id = ' . intval ( $id );
				if ($photoObject->update ( $data, $where )) {
					$status = 1;
				}
			}
			if ($status) {
				$this->message ( '数据没有任何更新' );
			}
			else {
				$this->ok ( '操作成功' );
			}
		
		}
		
		//获取照片列表信息
		if ($this->view->do == "") {
			$where = '';
			$numPerPage = 10;
			$page = $this->view->page ? $this->view->page : 1;
			$offset = $numPerPage * intval ( $page - 1 );
			
			$where = 'model_id= ' . $this->view->model_id;
			$paginator = Zend_Paginator::factory ( $photoObject->fetchAll ( $where ) );
			$this->view->photoList = $photoObject->fetchAll ( $where, 'photo_id DESC', $numPerPage, $offset );
			$paginator->setCurrentPageNumber ( $page )->setItemCountPerPage ( $numPerPage );
			$this->view->paginator = $paginator;
		
		}
	}
}

